package com.customview.TrianglePath;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/10/26 11:29
 */
public class CustomTrianglePath extends View implements View.OnClickListener {
    public CustomTrianglePath(Context context) {
        this(context, null);
    }

    public CustomTrianglePath(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTrianglePath(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 实现 有阴影的  自定义形状图
     */
    Paint mPaint;
    Paint textPaint;
    Path mPath;
    Path mLinePath;
    Paint mLinePaint;


    private void init() {

        mPaint = new Paint();
        mLinePaint = new Paint();
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(10);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPath = new Path();
        mLinePath = new Path();
        setOnClickListener(this);
        textPaint=new Paint();
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        float textwidth = textPaint.measureText("Android");
        Log.i("test1", "init: "+top+"  "+bottom+" "+textwidth);
    }


    int DefaultWidth = 100;
    int DefaultHeght = 100;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);


        switch (modeWidth) {

            case MeasureSpec.AT_MOST: //warp-parent

                break;
            case MeasureSpec.EXACTLY: //match_parent  或者 自定义

                DefaultWidth = sizeWidth;
                break;

        }
        switch (modeHeight) {

            case MeasureSpec.AT_MOST: //warp-parent

                break;
            case MeasureSpec.EXACTLY: //match_parent  或者 自定义

                DefaultHeght = sizeHeight;
                break;

        }

        setMeasuredDimension(DefaultWidth, DefaultHeght);


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        initPath();
    }

    List<Integer> datas;
    ValueAnimator valueAnimator;
    private void initPath() {

        final int measuredWidth = getMeasuredWidth();
        final int measuredHeight = getMeasuredHeight();
        mPaint.setAntiAlias(true);
        mPaint.setShader(new LinearGradient(0, 0, 0, measuredHeight, Color.BLUE, Color.GREEN, Shader.TileMode.CLAMP));


        final int dos = 30;
        final int unitWidth = measuredWidth / dos;
        datas = new ArrayList<>();
        for (int i = 0; i < dos; i++) {
            datas.add((int) (Math.random() * measuredHeight));
        }

         valueAnimator = ValueAnimator.ofInt(0, dos);
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();


                    Log.i("test", "onAnimationUpdate: " + animatedValue);
                    mPath.reset();//清除数据
                    mLinePath.reset();
                    mPath.moveTo(0, measuredHeight);
                   mLinePath.moveTo(0, measuredHeight);
                    for (int i = 0; i < animatedValue; i++) {
                        mPath.lineTo(unitWidth * i, datas.get(i));
                        mLinePath.lineTo(unitWidth * i, datas.get(i));

                        if (i == animatedValue - 1) {
                            mPath.lineTo(unitWidth * i, measuredHeight);

                        }
                    }
                    postInvalidate();
                }

        });
        valueAnimator.start();


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath, mPaint);
        canvas.drawPath(mLinePath, mLinePaint);

    }

    @Override
    public void onClick(View v) {
        if (!valueAnimator.isRunning()) {
            valueAnimator.start();
        }
    }
}
